(function($){
    /**
     * 仅允许拷贝纯文本.
     */
    $.fn.onlyPastePlainText = function(){
        $(this).on('paste', function(e) {        
            var text = e.originalEvent.clipboardData.getData("text/plain").nl2br();        
            document.execCommand("insertHTML", false, text);
            return false;
        });  
    };
    
    /**
     * 过虑显示包含指定text的节点.
     * @param {String} text
     * @param {Selector} textSelector
     * @param {Boolean} isHighlight 是否高亮显示，默认true.
     */
    $.fn.filterByText = function(text, textSelector, isHighlight){
        text = text.trim();
        $(this).each(function(){
            var $item = $(this),
                $filterConditionEle = textSelector ? $item.find(textSelector) : $item,
                wholeText = $filterConditionEle.text();
            if(!text || wholeText.contains(text)){
                if(text && isHighlight !== false){
                    //html的参数需要是 safe string, 避免XSS漏洞。
                    $filterConditionEle.html(wholeText.highlight(text.safeString()));
                } else {
                    $filterConditionEle.text(wholeText);
                }
                $item.show();
            } else {
                $item.hide();
            }
        });    
    };
    
    /**
     * 切换元素的text.
     */
    $.fn.toggleText = function(text, alternativeText){
        var $self = $(this),
            currentText = $self.text();    
        if(!text){
            text = $self.text();
            alternativeText = $self.attr('alternativetext');
            $self.attr('alternativetext', text);
        }
        $self.text( $.trim(currentText) != $.trim(text) ? text : alternativeText );
        return this;    
    };
    
    /**
     * 预加载
     * TODO: 非图片类型的预加载?
     */
    var _imgCache = {};  
    /**
     * 图片预加载
     */
    $.preLoadImages = function() {
        var imgCount = arguments.length,
            createEle = document.createElement;
        for (var i = imgCount; i--;) {
            var imgPath = arguments[i];
            if(!_imgCache[imgPath]){
                var cachedImg = createEle.call(document, 'img');                 
                cachedImg.src = imgPath;
                _imgCache[imgPath] = cachedImg;
            }
        }
        return _imgCache;
    };
    //test demo.
    //$.preLoadImages("image1.gif", "/path/to/image2.png");
    
})(jQuery);